package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import keeto.com.vn.bms.config.Config;
import keeto.com.vn.bms.ui.adapter.HomeBMSTabAdpter;
import keeto.com.vn.bms.ui.fragment.BaseFragment;
import keeto.com.vn.bms.ui.fragment.CategoryBMSHomeFragment;
import keeto.com.vn.bms.ui.fragment.ContactBMSFragment;
import keeto.com.vn.bms.ui.fragment.BMSDataEntryFragment;
import keeto.com.vn.bms.ui.fragment.InfoBMSHomeFragment;
import keeto.com.vn.bms.ui.widget.LogoutDialog;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;
import keeto.com.vn.bms.viewpagerindicator.TabPageIndicator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class BMSAdminActivity extends FragmentActivity implements OnClickListener{

	private ImageButton btn_tittle_back;
	private TextView tv_title_name;
	private ImageButton btn_tittle_login;
	private TabPageIndicator tab_admin_indicator;
	private ViewPager admin_pager;

	private ArrayList<BaseFragment> listFragment;
	private HomeBMSTabAdpter mHomeBMSAdpter;
	private FragmentManager mMageger;
	private BMSSharedPreferences preferences;


	public static void actionLaunch(Context context) {
		Intent intent = new Intent(context, BMSAdminActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.admin_activity_layout);
		preferences = new BMSSharedPreferences(this);

		mMageger = getSupportFragmentManager();
		initUIComponent();
	}

	private void initUIComponent(){
		btn_tittle_back = (ImageButton)findViewById(R.id.btn_tittle_back);
		tv_title_name = (TextView)findViewById(R.id.tv_title_name);
		btn_tittle_login = (ImageButton)findViewById(R.id.btn_tittle_login);
		tab_admin_indicator = (TabPageIndicator)findViewById(R.id.tab_admin_indicator);
		admin_pager = (ViewPager)findViewById(R.id.admin_pager);

		btn_tittle_back.setOnClickListener(this);
		btn_tittle_login.setOnClickListener(this);
		tv_title_name.setText(this.getString(R.string.category_admin));
		btn_tittle_login.setBackgroundResource(Utils.getLogobyPermission(preferences.getUserPermission(this)));

		listFragment = new ArrayList<BaseFragment>();
		ContactBMSFragment contactBMSFragment = ContactBMSFragment.newInstance();
		contactBMSFragment.setTitle(this.getString(R.string.tittle_contact));
		
		BMSDataEntryFragment bMSDataEntryFragment = BMSDataEntryFragment.newInstance();
		bMSDataEntryFragment.setTitle(this.getString(R.string.tittle_dataEntry));
		
		listFragment.add(contactBMSFragment);
		listFragment.add(bMSDataEntryFragment);
		mHomeBMSAdpter = new HomeBMSTabAdpter(mMageger, listFragment);
		admin_pager.setAdapter(mHomeBMSAdpter);
		tab_admin_indicator.setViewPager(admin_pager);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			this.finish();
			break;
		case R.id.btn_tittle_login:
			if (preferences.getUserAuthKey(BMSAdminActivity.this).equals("")){
				LoginBMSActivity.actionLaunch(BMSAdminActivity.this);
			}else{
				LogoutDialog dialog = new LogoutDialog(BMSAdminActivity.this);
				dialog.show();
			}
			break;
		default:
			break;
		}
	}




}
